package com.mybatis.jpa.statement.builder;

import com.mybatis.jpa.meta.MybatisColumnMeta;
import com.mybatis.jpa.meta.PersistentMeta;
import com.mybatis.jpa.statement.MybatisStatementAdapter;
import com.mybatis.jpa.statement.SqlAssistant;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.SqlCommandType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * 普通插入
 * @author wanglei
 * @date 2020 -02-06 11:06:32
 */
public class InsertBuilder extends BaseInsertBuilder{

	@Override
	public String buildSQL(final PersistentMeta persistentMeta, Method method) {
		return new SQL() {
			{
				INSERT_INTO(persistentMeta.getTableNameFlag());
				for (MybatisColumnMeta columnMeta : persistentMeta.getColumnMetaMap().values()) {
					if(columnMeta.isVersion())
					{
						VALUES(columnMeta.getColumnName(), "0");
					}
					else
					{
						VALUES(columnMeta.getColumnName(), SqlAssistant.resolveSqlParameter(columnMeta));
					}

				}
			}
		}.toString();
	}

	@Override
	public void parseStatement(MybatisStatementAdapter adapter, PersistentMeta persistentMeta, Method method) {
		// 方法名
		adapter.setMethodName("insertJpa");
		// 参数类型
		adapter.setParameterTypeClass(persistentMeta.getType());
		// sqlScript
		adapter.setSqlScript(buildSQL(persistentMeta, method));
		super.setKeyGenerator(adapter,persistentMeta,method);
		adapter.parseStatement();
	}

}
